package cn.flying.cloud.task.controller;

import javax.annotation.Resource;

import java.util.List;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.flying.cloud.base.common.vo.PageRequest;
import cn.flying.cloud.base.common.vo.PageResponse;
import cn.flying.cloud.base.common.vo.Rt;
import cn.flying.cloud.task.entity.TaskStepInfo;
import cn.flying.cloud.task.service.TaskStepInfoService;

/**
 * 批量任务step信息 前端控制器
 *
 * @author: admin
 * @date: 2025-04-27 16:36:53
 * @version: 1.0
 */
@Controller
@RequestMapping("/task/step/info")
public class TaskStepInfoController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private TaskStepInfoService taskStepInfoService;

    /**
     * 跳转到列表页面
     *
     * @return
     */
    @RequestMapping("index")
    public String index(Model model) {
//        Map<String, String> statusList = codeService.getCodeList("status");
//        model.addAttribute("statusList", statusList);
        return "task/job/index";
    }

    /**
     * 分页查询批量任务step信息数据
     *
     * @param request
     * @return
     */
    @PostMapping("page")
    @ResponseBody
    public PageResponse<TaskStepInfo> page(PageRequest<TaskStepInfo> request) {
        return taskStepInfoService.getPageList(request);
    }

    /**
     * 列表查询批量任务step信息数据
     *
     * @param taskStepInfo
     * @return
     */
    @PostMapping("list")
    @ResponseBody
    public Rt<?> list(@RequestBody TaskStepInfo taskStepInfo) {
        List<TaskStepInfo> list = taskStepInfoService.getList(taskStepInfo);
        return Rt.success(list);
    }

    /**
     * 查询批量任务step信息详情数据
     *
     * @param taskStepInfo
     * @return
     */
    @PostMapping("detail")
    @ResponseBody
    public Rt<?> detail(TaskStepInfo taskStepInfo) {
        return Rt.success(taskStepInfoService.getOneObj(taskStepInfo));
    }

    /**
     * 根据主键ID查询批量任务step信息详情数据
     *
     * @param id 主键ID
     * @return
     */
    @PostMapping("getById")
    @ResponseBody
    public Rt<?> getById(Long id) {
        TaskStepInfo taskStepInfo = taskStepInfoService.getById(id);
        return Rt.success(taskStepInfo);
    }

    /**
     * 新增/修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("turn/edit")
    public String turnEdit(Long id, Model model) {
        if (null != id) {
            TaskStepInfo formData = taskStepInfoService.getById(id);
            model.addAttribute("formData", formData);
        } else {
            TaskStepInfo formData = new TaskStepInfo();
//            formData.setStatus("Y");
            model.addAttribute("formData", formData);
        }
        return "task/job/edit";
    }

    /**
     * 保存批量任务step信息数据
     *
     * @param taskStepInfo
     * @return
     */
    @PostMapping("save")
    @ResponseBody
    public Rt<?> save(@RequestBody TaskStepInfo taskStepInfo) {
        if (Objects.isNull(taskStepInfo.getId())) {
            // 新增
            taskStepInfoService.createObj(taskStepInfo);
        } else {
            // 更新
            taskStepInfoService.updateObj(taskStepInfo);
        }
        return Rt.success("保存成功！");
    }

    /**
     * 删除批量任务step信息数据
     *
     * @param taskStepInfo
     * @return
     */
    @PostMapping("delete")
    @ResponseBody
    public Rt<?> delete(TaskStepInfo taskStepInfo) {
        taskStepInfoService.deleteObj(taskStepInfo);
        return Rt.success("删除成功！");
    }

    /**
     * 根据主键ID删除批量任务step信息数据
     *
     * @param id 主键ID
     * @return
     */
    @GetMapping("deleteById/{id}")
    @ResponseBody
    public Rt<?> deleteById(@PathVariable("id") Long id) {
        taskStepInfoService.deleteById(id);
        return Rt.success("删除成功！");
    }
}
